package org.leiyang.common.coder;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import org.leiyang.common.dtos.Message;
import org.leiyang.common.util.ProtostuffUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class MsgDecoder extends ByteToMessageDecoder {
    private final Logger logger = LoggerFactory.getLogger(getClass());
    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        try {
            byte[] body = new byte[byteBuf.readableBytes()];  //传输正常
            byteBuf.readBytes(body);

            list.add(ProtostuffUtils.deserialize(body, Message.class));

        } catch (Exception e) {
            logger.error("反序列化Message失败, ", e);
        }
    }
}
